package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Level;
import com.woniu.pojo.Member;
import com.woniu.service.IBookService;
import com.woniu.service.ILevelService;
import com.woniu.service.IMemberService;
import com.woniu.utils.MobileUtil;
import com.woniu.utils.SendSmsUtil;
import com.woniu.vo.BookQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dong
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录页面")
@Slf4j
public class LoginController extends BaseController{

    @Autowired
    private IMemberService service;

    @Autowired
    private ILevelService levelService;

    @GetMapping("/sendMail/{tel}")
    @ApiOperation("发送短信")
    public Result sendMail(@PathVariable String tel, HttpSession session){
        boolean flag = MobileUtil.checkPhone(tel);
        if(!flag){
            return error("请输入正确的手机号码");
        }
        String code = SendSmsUtil.getRandomNum(4);
        log.debug(code);
        session.setAttribute("code",code);
        session.setAttribute("register_tel",tel);
        return success("发送短信成功");
    }

    @GetMapping("/checkSms/{tel}/{code}")
    @ApiOperation("登录/注册")
    @ResponseBody
    public Result checkSmsCode(@PathVariable String code, @PathVariable String tel, HttpSession session){
        String sessionCode = (String) session.getAttribute("code");
        String registerTel= (String) session.getAttribute("register_tel");
        if(registerTel==null||registerTel.length()==0){
            return error("请输入手机号码");
        }
        if(!registerTel.equals(tel)){
            return error("请输入登录时的手机号码");
        }
        if(sessionCode==null||sessionCode.length()==0){
            return error("未发送验证码，请发送验证码");
        }
        if(!sessionCode.equals(code)){
            return error("验证码错误，请重新输入");
        }
        QueryWrapper wrapper= Wrappers.query();
        wrapper.eq("tel",tel);
        List list = service.list(wrapper);
        if(list==null||list.size()==0){
            Member member=new Member();
            member.setTel(tel);
            boolean flag = service.save(member);
            if(!flag){
                return error();
            }
            return success("注册成功");
        }
        session.setAttribute("tel",tel);
        List<Member> newList = service.list(wrapper);
        for (Member member : newList) {
            session.setAttribute("member",member);
            Level level = levelService.getById(member.getLevelId());
            session.setAttribute("discount",level.getDiscount());
        }
        session.removeAttribute("code");
        return success("登录成功");
    }



}
